package com.jhgsys.internal.hazard.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.jhgsys.internal.common.annotation.Log;
import com.jhgsys.internal.common.controller.BaseController;
import com.jhgsys.internal.common.entity.FebsConstant;
import com.jhgsys.internal.common.entity.FebsResponse;
import com.jhgsys.internal.common.entity.QueryRequest;
import com.jhgsys.internal.common.entity.FebsConstant;
import com.jhgsys.internal.common.exception.FebsException;
import com.jhgsys.internal.common.utils.BeanHelper;
import com.jhgsys.internal.common.utils.FebsUtil;
import com.jhgsys.internal.hazard.dto.HazardExamineConfigDto;
import com.jhgsys.internal.hazard.entity.HazardExamineConfig;
import com.jhgsys.internal.hazard.entity.HazardExamineDtlConfig;
import com.jhgsys.internal.hazard.entity.HazardReportConfig;
import com.jhgsys.internal.hazard.service.IHazardExamineConfigService;
import com.jhgsys.internal.hazard.service.IHazardExamineDtlConfigService;
import com.wuwenze.poi.ExcelKit;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;
import java.util.Map;
import javax.validation.constraints.NotBlank;
import java.util.Date;
import java.util.stream.Collectors;

/**
 *  Controller
 *
 * @author Lujiachen
 * @date 2024-08-13 08:53:55
 */
@Slf4j
@Validated
@Controller
public class HazardExamineConfigController extends BaseController {

    @Autowired
    private IHazardExamineConfigService hazardExamineConfigService;

    @Autowired
    private IHazardExamineDtlConfigService hazardExamineDtlConfigService;

    @GetMapping(FebsConstant.VIEW_PREFIX + "hazardExamineConfig")
    @RequiresPermissions("hazardExamineConfig:view")
    public String hazardExamineConfigIndex(Model model){
        model.addAttribute("tenantList", getTenantList());
        model.addAttribute("tenantId", getCurrentTenantId());
        model.addAttribute("hasHeadCompanyRole", hasHeadCompanyRole());
        if(StringUtils.isNotBlank(getCurrentTenantId())){
            List<HazardExamineConfig> hazardExamineConfigList = hazardExamineConfigService.findHazardExamineConfigListByTenantId(getCurrentTenantId());
            List<HazardExamineConfigDto> hazardExamineConfigDtos = BeanHelper.copyWithCollection(hazardExamineConfigList,HazardExamineConfigDto.class);
            hazardExamineConfigDtos = hazardExamineConfigDtos.stream().map(d ->{
                String configId = d.getExamineConfigId();
                HazardExamineDtlConfig hazardExamineDtlConfig = new HazardExamineDtlConfig();
                hazardExamineDtlConfig.setExamineConfigId(configId);
                List<HazardExamineDtlConfig> dtlConfigList = hazardExamineDtlConfigService.findHazardExamineDtlConfigListByExamineConfigId(hazardExamineDtlConfig);
                d.setDtlConfigList(dtlConfigList);
                        return d;
            }).collect(Collectors.toList());
            model.addAttribute("hazardExamineConfigList",hazardExamineConfigDtos);
        }
        return FebsUtil.view("hazard/hazardExamineConfig/hazardExamineConfigList");
    }

    @GetMapping(FebsConstant.VIEW_PREFIX + "hazardExamineConfig/add")
    @RequiresPermissions("hazardExamineConfig:add")
    public String hazardExamineConfigAdd(){
        return FebsUtil.view("hazard/hazardExamineConfig/hazardExamineConfigEdit");
    }

    @GetMapping(FebsConstant.VIEW_PREFIX + "hazardExamineConfig/update/{id}")
    @RequiresPermissions("hazardExamineConfig:edit")
    public String hazardExamineConfigUpdate(Model model, @PathVariable("id") String id){
        model.addAttribute("hazardExamineConfig", hazardExamineConfigService.findHazardExamineConfigById(id));
        return FebsUtil.view("hazard/hazardExamineConfig/hazardExamineConfigEdit");
    }

    /**
     * 查询（所有）
     * @author Lujiachen
     * @date 2024-08-13 08:53:55
     */
    @GetMapping("hazardExamineConfig")
    @ResponseBody
    @RequiresPermissions("hazardExamineConfig:view")
    public FebsResponse getAllHazardExamineConfigs(HazardExamineConfig hazardExamineConfig) {
        return new FebsResponse().success().data(hazardExamineConfigService.findHazardExamineConfigList(hazardExamineConfig));
    }

    /**
     * 查询（分页）
     * @author Lujiachen
     * @date 2024-08-13 08:53:55
     */
    @GetMapping("hazardExamineConfig/list")
    @ResponseBody
    @RequiresPermissions("hazardExamineConfig:view")
    public FebsResponse hazardExamineConfigList(QueryRequest request, HazardExamineConfig hazardExamineConfig) {
        Map<String, Object> dataTable = getDataTable(this.hazardExamineConfigService.findHazardExamineConfigPage(request, hazardExamineConfig));
        return new FebsResponse().success().data(dataTable);
    }

    /**
     * 新增 HazardExamineConfig
     * @author Lujiachen
     * @date 2024-08-13 08:53:55
     */
    @Log("新增HazardExamineConfig")
    @PostMapping("hazardExamineConfig")
    @ResponseBody
    @RequiresPermissions("hazardExamineConfig:add")
    public FebsResponse addHazardExamineConfig(@RequestBody HazardExamineConfig hazardExamineConfig) throws FebsException {
        try {
            hazardExamineConfig.setCreatedBy(getCurrentUser().getUserName());
            this.hazardExamineConfigService.createHazardExamineConfig(hazardExamineConfig);
            return new FebsResponse().success();
        } catch (Exception e) {
            String message = "新增HazardExamineConfig失败";
            log.error(message, e);
            return failure(e,message);
        }
    }

    /**
     * 逻辑删除 HazardExamineConfig
     * @author Lujiachen
     * @date 2024-08-13 08:53:55
     */
    @Log("逻辑删除HazardExamineConfig")
    @GetMapping("hazardExamineConfig/delete/{id}")
    @ResponseBody
    @RequiresPermissions("hazardExamineConfig:delete")
    public FebsResponse deleteHazardExamineConfig(@PathVariable("id") String hazardExamineConfigIds) throws FebsException {
        try {
            HazardExamineConfig hazardExamineConfig = new HazardExamineConfig();
            hazardExamineConfig.setDeletedBy(getCurrentUser().getUserName());
            hazardExamineConfig.setDeletedTime(new Date());
            hazardExamineConfig.setDeletedFlg(FebsConstant.INVALID);
            this.hazardExamineConfigService.deleteHazardExamineConfig(hazardExamineConfig,hazardExamineConfigIds);
            return new FebsResponse().success();
        } catch (Exception e) {
            String message = "删除HazardExamineConfig失败";
            log.error(message, e);
            return failure(e,message);
        }
    }

    /**
    * 物理删除 HazardExamineConfig
    * @author Lujiachen
    * @date 2024-08-13 08:53:55
    */
    @Log("物理删除HazardExamineConfig")
    @GetMapping("hazardExamineConfig/physicallyDelete/{id}")
    @ResponseBody
    @RequiresPermissions("hazardExamineConfig:delete")
    public FebsResponse physicallyDeleteHazardExamineConfig(@PathVariable("id") String hazardExamineConfigIds) throws FebsException {
        try {
            this.hazardExamineConfigService.physicallyDeleteHazardExamineConfig(hazardExamineConfigIds);
            return new FebsResponse().success();
        } catch (Exception e) {
            String message = "删除HazardExamineConfig失败";
            log.error(message, e);
            return failure(e,message);
        }
    }

    /**
     * 修改 HazardExamineConfig
     * @author Lujiachen
     * @date 2024-08-13 08:53:55
     */
    @Log("修改HazardExamineConfig")
    @PostMapping("hazardExamineConfig/update")
    @ResponseBody
    @RequiresPermissions("hazardExamineConfig:edit")
    public FebsResponse updateHazardExamineConfig(HazardExamineConfig hazardExamineConfig) throws FebsException {
        try {
            hazardExamineConfig.setUpdatedBy(getCurrentUser().getUserName());
            this.hazardExamineConfigService.updateHazardExamineConfig(hazardExamineConfig);
            return new FebsResponse().success();
        } catch (Exception e) {
            String message = "修改HazardExamineConfig失败";
            log.error(message, e);
            return failure(e,message);
        }
    }

    @GetMapping(FebsConstant.VIEW_PREFIX + "hazardExamineConfig/selectDept")
    @RequiresPermissions("hazardExamineConfig:view")
    public String hazardExamineConfigSelectDept(Model model){
        model.addAttribute("tenantList", getTenantList());
        model.addAttribute("tenantId", getCurrentTenantId());
        model.addAttribute("hasHeadCompanyRole", hasHeadCompanyRole());
        return FebsUtil.view("hazard/hazardExamineConfig/hazardExamineConfigSelectDept");
    }



    /**
     * 新增 HazardExamineConfigList
     * @author Lujiachen
     * @date 2024-08-13 08:53:55
     */
    @Log("新增HazardExamineConfigList")
    @PostMapping("hazardExamineConfigList")
    @ResponseBody
    @RequiresPermissions("hazardExamineConfig:view")
    public FebsResponse addHazardExamineConfigList(@RequestBody List<HazardExamineConfigDto> hazardExamineConfigDtoList) throws FebsException {
        try {
             for(HazardExamineConfigDto hazardExamineConfigDto:hazardExamineConfigDtoList) {
                hazardExamineConfigDto.setCreatedBy(getCurrentUser().getUserName());
            }
            this.hazardExamineConfigService.createHazardExamineConfigList(hazardExamineConfigDtoList);
            return new FebsResponse().success();
        } catch (Exception e) {
            String message = "新增HazardExamineConfig失败";
            log.error(message, e);
            return failure(e,message);
        }
    }


    /**
     * 修改 HazardExamineConfigList
     * @author Lujiachen
     * @date 2024-08-13 08:53:55
     */
    @Log("修改HazardExamineConfig")
    @PostMapping("hazardExamineConfigList/update")
    @ResponseBody
    @RequiresPermissions("hazardExamineConfig:view")
    public FebsResponse updateHazardExamineConfigList(@RequestBody List<HazardExamineConfigDto> hazardExamineConfigDtoList) throws FebsException {
        try {
            for(HazardExamineConfigDto hazardExamineConfigDto:hazardExamineConfigDtoList) {
                hazardExamineConfigDto.setUpdatedBy(getCurrentUser().getUserName());
            }
            this.hazardExamineConfigService.updateHazardExamineConfigList(hazardExamineConfigDtoList);
            return new FebsResponse().success();
        } catch (Exception e) {
            String message = "修改HazardExamineConfig失败";
            log.error(message, e);
            return failure(e,message);
        }
    }


    /**
     * 查询（公司id）
     * @author Lujiachen
     * @date 2024-08-13 19:50:23
     */
    @GetMapping("hazardExamineConfig/search/{id}")
    @ResponseBody
    @RequiresPermissions("hazardExamineConfig:view")
    public FebsResponse getIncentiveRewardConfigById(Model model, @PathVariable("id") String  id) {
        List<HazardExamineConfig> hazardExamineConfigList = hazardExamineConfigService.findHazardExamineConfigListByTenantId(id);
        List<HazardExamineConfigDto> hazardExamineConfigDtos = BeanHelper.copyWithCollection(hazardExamineConfigList,HazardExamineConfigDto.class);
        hazardExamineConfigDtos = hazardExamineConfigDtos.stream().map(d ->{
            String configId = d.getExamineConfigId();
            HazardExamineDtlConfig hazardExamineDtlConfig = new HazardExamineDtlConfig();
            hazardExamineDtlConfig.setExamineConfigId(configId);
            List<HazardExamineDtlConfig> dtlConfigList = hazardExamineDtlConfigService.findHazardExamineDtlConfigListByExamineConfigId(hazardExamineDtlConfig);
            d.setDtlConfigList(dtlConfigList);
            return d;
        }).collect(Collectors.toList());
        model.addAttribute("hazardExamineConfigList",hazardExamineConfigDtos);
        return new FebsResponse().success().data(model);
    }


    @GetMapping(FebsConstant.VIEW_PREFIX + "hazardExamineConfig/selectProject")
    @RequiresPermissions("hazardExamineConfig:view")
    public String hazardExamineConfigSelectProject(Model model){
        model.addAttribute("tenantList", getTenantList());
        model.addAttribute("tenantId", getCurrentTenantId());
        model.addAttribute("hasHeadCompanyRole", hasHeadCompanyRole());
        return FebsUtil.view("hazard/hazardExamineConfig/hazardExamineConfigSelectProj");
    }

}
